<?php

namespace App\Service\Message\v1_3_4;

use App\Cache\UserRemarkCache;
use App\Helpers\DateUtil;
use App\Model\User;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Service\BaseService;

/**
 * 漏接通话列表
 */
class LeakCallService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $this->userId = $userId;
        $ret = ['list' => []];
        $data = VideoCall::getLeakList($userId, 1);
        if (!$data || $data[0]['read']) return $ret;
        $callUserIds = array_column($data, 'user_id');
        $usersProfile = UserProfile::getUsersProfile($callUserIds, true);
        $imAccounts = User::getImAccounts($callUserIds);
        foreach ($data as $item) {
            $ret['list'][] = [
                'user_id' => $item['user_id'],
                'nick_name' => $usersProfile[$item['user_id']]['nick_name'],
                'gender' => $usersProfile[$item['user_id']]['gender'],
                'avatar' => UserProfile::buildAvatar($usersProfile[$item['user_id']]['avatar'], 200),
                'age' => DateUtil::dateDiff($usersProfile[$item['user_id']]['birthday'], date('Y-m-d'), 'Y'),
                'im_account' => $imAccounts[$item['user_id']],
                'call_time' => (string)$item['created_at'],
                'call_type' => 1,
                'remark' => $this->userId != $item['user_id'] ? (new UserRemarkCache())->getRemark($this->userId, $item['user_id']) : '',
            ];
        }
        return $ret;
    }
}
